
package com.hazelcast.test.jdbc;

import org.testcontainers.containers.JdbcDatabaseContainer;

import javax.sql.CommonDataSource;
import java.util.Properties;

public abstract class JdbcDatabaseProvider<C extends JdbcDatabaseContainer<?>> implements TestDatabaseProvider {

    private static final int LOGIN_TIMEOUT = 120;

    protected C container;

    public abstract CommonDataSource createDataSource(boolean xa);

    abstract C createContainer(String dbName);

    @Override
    public String createDatabase(String dbName) {
        container = createContainer(dbName);
        container.start();
        String jdbcUrl = url();
        waitForDb(jdbcUrl, LOGIN_TIMEOUT);
        return jdbcUrl;
    }

    @Override
    public String noAuthJdbcUrl() {
        return container.getJdbcUrl().replaceAll("&?user=root", "").replaceAll("&?password=test", "");
    }

    public String getDatabaseName() {
        return container.getDatabaseName();
    }

    @Override
    public String url() {
        return container.getJdbcUrl();
    }

    @Override
    public String user() {
        return "root";
    }

    @Override
    public String password() {
        return "test";
    }

    public C container() {
        return container;
    }

    @Override
    public void shutdown() {
        if (container != null) {
            container.stop();
            container = null;
        }
    }

    @Override
    public Properties properties() {
        Properties properties = new Properties();
        properties.setProperty("jdbcUrl", url());
        return properties;
    }

    @Override
    public TestDatabaseRecordProvider recordProvider() {
        return new JdbcObjectProvider(this);
    }
}
